//
//  LDOriginalView.swift
//  Weibo201655
//
//  Created by pro on 16/5/10.
//  Copyright © 2016年 LeeDan. All rights reserved.
//

import UIKit
import SDWebImage

let LDStatusCellMargin: CGFloat = 8.0//间距
class LDOriginalView: UIView {

    //MARK:----------------------- 属性 ----------------------------------//
    //微博属性
    var status: LDStatus? {
        didSet {
            //用户头像
            if let iconUrlStr = status?.user?.profile_image_url {
                let iconURL = NSURL(string: iconUrlStr)
                iconView.sd_setImageWithURL(iconURL, placeholderImage: UIImage(named: "avatar"), options: SDWebImageOptions.RetryFailed)
            }
            //设置用户名
            nameLabel.text = status?.user?.screen_name
            //设置时间
            timeLabel.text = status?.created_at
            //设置来源
            sourceLabel.text = status?.source
            //设置微博内容
            contentLabel.text = status?.text
            // 设置会员等级
            if let brank = status?.user?.mbrank {
                if brank > 0 && brank < 6 {
                    memberView.image = UIImage(named: "common_icon_membership_level\(brank)")
                }else {
                    memberView.image = nil
                }
            }
            
            // 设置认证图标
            if let type = status?.user?.verified_type {
                var img: UIImage? = nil
                switch type {
                case 0:
                    img = UIImage(named: "avatar_vip")
                case 2,3,5:
                    img = UIImage(named: "avatar_enterprise_vip")
                case 220:
                    img = UIImage(named: "avatar_grassroot")
                default:
                    img = nil
                }
                verifiedTypeImage.image = img
            }
            
            //设置配图
            pictureView.status = status
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 1.添加子控件
        addSubview(iconView)
        addSubview(verifiedTypeImage)
        addSubview(nameLabel)
        addSubview(memberView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
         // 2.添加约束
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(LDStatusCellMargin)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        //认证类型
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        //用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(LDStatusCellMargin)
            make.top.equalTo(self).offset(LDStatusCellMargin)
        }
        //会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(LDStatusCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSizeMake(14, 14))
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(LDStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        //微博来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(LDStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        //微博内容的label
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(LDStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(LDStatusCellMargin)
            make.trailing.equalTo(self).offset(-LDStatusCellMargin)
        }
        
        //配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(LDStatusCellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(LDStatusCellMargin)
            make.size.equalTo(CGSizeMake(200, 200))
        }
        //设置originView的底部和contentLabel的底部重合
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(LDStatusCellMargin)
        }
    }
    
    
    //MARK: 懒加载
    //头像
    private var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    //用户名称
    private var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), font: UIFont.systemFontOfSize(14))
    //会员等级
    private var memberView: UIImageView = UIImageView(image: nil)
    //时间
    private var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), font: UIFont.systemFontOfSize(11))
    //微博来源
    private var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), font: UIFont.systemFontOfSize(11))
    //认证类型
    private var verifiedTypeImage: UIImageView = UIImageView(image: nil)
    //微博内容的label
    private var contentLabel: UILabel = {
    
        let label = UILabel(textColor: UIColor.darkGrayColor(), font: UIFont.systemFontOfSize(15))
        
        //设置
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - LDStatusCellMargin * 2
        
        return label
    }()
    
    //配图
    private lazy var pictureView: LDCellCollectionView = LDCellCollectionView()
    

}
